{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9cb2df2c",
   "metadata": {},
   "source": [
    "——————————————————————————\n",
    "\n",
    "* 本学习记录人：林涵萩\n",
    "* 记录时间：2021-11-23\n",
    "* week13\n",
    "* 参考文档：\n",
    "> 1. [python官方文档](https://www.python.org/doc/)\n",
    "> 2. [许老师博客](https://www.cnblogs.com/xuzhichao)\n",
    "\n",
    "——————————————————————————"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c870b52c",
   "metadata": {},
   "source": [
    "## 1. 列表实践之购物车\n",
    "\n",
    "```\n",
    "购物车程序项目：\n",
    "\n",
    "要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　   2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　   3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "　　   4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d201cc3",
   "metadata": {},
   "source": [
    "-----------------------------------------------------\n",
    "\n",
    "* 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "76a170fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：1000\n"
     ]
    }
   ],
   "source": [
    "price = input(\"请输入您的余额：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "d21d95ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "things = [\"123456,456752,336454\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b4db0e53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['面巾纸，10元，123456', '矿泉水，3元，456752', '抹布，1元，336454']\n"
     ]
    }
   ],
   "source": [
    "商品1 = \"面巾纸，10元，123456\"\n",
    "商品2 = \"矿泉水，3元，456752\"\n",
    "商品3 = \"抹布，1元，336454\"\n",
    "商品列表 = [\n",
    "    商品1,\n",
    "    商品2,\n",
    "    商品3\n",
    "       ]\n",
    "\n",
    "print(商品列表)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "d8563b7d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入编号购买商品：123456\n",
      "您输入的商品不存在！\n"
     ]
    }
   ],
   "source": [
    "i = input(\"请输入编号购买商品：\")\n",
    "\n",
    "if i in things:\n",
    "    print(\"商品已添加！\")\n",
    "else:\n",
    "    print(\"您输入的商品不存在！\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "997da003",
   "metadata": {},
   "source": [
    "---------------------------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f5fd383",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 商品信息：列表数据\n",
    "# goods_list\n",
    "goods_list = [\n",
    "    ['珍珠奶茶',6],\n",
    "    ['柠檬茶大杯',4],\n",
    "    ['奶盖四季柠檬茶',8],\n",
    "    ['杨枝甘露',7],\n",
    "    ['圣代',9]\n",
    "    \n",
    "]\n",
    "\n",
    "# 用户已购买的商品：列表数据\n",
    "user_shopping_cart = []\n",
    "\n",
    "# 用户输入\n",
    "Alipay = int(input('请输入您的余额：'))\n",
    "\n",
    "# 打印商品信息\n",
    "# ---------------\n",
    "# 商品编号：%s ,商品名称：%s，商品价格：%s\n",
    "\n",
    "while True:\n",
    "    print(\"--------商品列表--------\")\n",
    "    print(\"商品编号：%s ,商品名称：%s，商品价格：%s\"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "    print(\"商品编号：%s ,商品名称：%s，商品价格：%s\"%(1,goods_list[1][0],goods_list[1][1]))\n",
    "    print(\"商品编号：%s ,商品名称：%s，商品价格：%s\"%(2,goods_list[2][0],goods_list[2][1]))\n",
    "    print(\"商品编号：%s ,商品名称：%s，商品价格：%s\"%(3,goods_list[3][0],goods_list[3][1]))\n",
    "    print(\"商品编号：%s ,商品名称：%s，商品价格：%s\"%(4,goods_list[4][0],goods_list[4][1]))\n",
    "\n",
    "    choice = input(\"请输入你要购买的商品编号(输入‘q’退出购买):\")          \n",
    "                   #  1. 输入编号数字 2. 输入“q”\n",
    "    if choice.isdigit():\n",
    "        if int(choice)>=0 and int(choice)<len(goods_list):\n",
    "            # 一、数字在列表范围内\n",
    "            # 1. 输入编号数字\n",
    "            # 判断1：Alipay VS goods_list[choice][1](商品价格)、\n",
    "            if Alipay >= goods_list[int(choice)][1]:\n",
    "                Alipay = Alipay - goods_list[int(choice)][1]# 扣钱\n",
    "                print(\"你点的【%s】已经购买成功，请等候，你的余额还有【%s】\"%(goods_list[int(choice)][0],Alipay))\n",
    "                user_shopping_cart.append(goods_list[int(choice)])\n",
    "            else:\n",
    "                print(\"你的钱包不够啊，还想喝奶茶，换种方式支付吧\")\n",
    "        else:\n",
    "            # 二、数字不在列表范围内\n",
    "            print(\"您输入的商品不存在，请输入正确的商品编码！（输入“q”退出购买）：\")\n",
    "    elif choice == \"q\":\n",
    "        # 2. 输入“q”\n",
    "        print(\"------购物车------\")\n",
    "        print(\"您的购物车为【%s】，您的余额还有【%s】元\"%(user_shopping_cart,Alipay))\n",
    "        break\n",
    "    else:\n",
    "            # 3. 输入的不符合要求\n",
    "            print(\"**您输入的内容不符合要求，请检查重新输入！**\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "06fc3e5c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "商品编号：0,商品名称：珍珠奶茶，商品价格：6\n",
      "商品编号：1,商品名称：柠檬茶大杯，商品价格：4\n",
      "商品编号：2,商品名称：奶盖四季柠檬茶，商品价格：8\n",
      "商品编号：3,商品名称：杨枝甘露，商品价格：7\n",
      "商品编号：4,商品名称：圣代，商品价格：9\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(\"商品编号：%s,商品名称：%s，商品价格：%s\"%(i,goods_list[i][0],goods_list[i][1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "15accbcf",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(goods_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "9355560d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "商品编号：0 ,商品名称：珍珠奶茶，商品价格：6\n",
      "商品编号：1 ,商品名称：柠檬茶大杯，商品价格：4\n",
      "商品编号：2 ,商品名称：奶盖四季柠檬茶，商品价格：8\n",
      "商品编号：3 ,商品名称：杨枝甘露，商品价格：7\n",
      "商品编号：4 ,商品名称：圣代，商品价格：9\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(goods_list)):\n",
    "    print(\"商品编号：%s ,商品名称：%s，商品价格：%s\"%(i,goods_list[i][0],goods_list[i][1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52a277a7",
   "metadata": {},
   "source": [
    "## 实践二"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fee5e2cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:········\n",
      "请输入数字在0-99之间。不要耍赖！\n"
     ]
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-        # ？？\n",
    "# Author:Zhichao\n",
    "#猜数字（0-100）\n",
    "\n",
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card:\"))\n",
    "count = 0\n",
    "\n",
    "if hide_card <100 and hide_card >0:\n",
    "    while True:\n",
    "        if count <5:\n",
    "            guess_digit = int(input(\"guess_digit:\"))\n",
    "            if guess_digit == hide_card:\n",
    "                print(\"yes, you got it.\")\n",
    "                break\n",
    "            elif guess_digit > hide_card:\n",
    "                print(\"think smaller...\")\n",
    "            else:\n",
    "                print(\"think bigger...\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜这么多次都不对,你个笨蛋!\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "else:\n",
    "    print(\"请输入数字在0-99之间。不要耍赖！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "8fbcb378",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:········\n",
      "请输入数字在0-99之间。不要耍赖！\n"
     ]
    }
   ],
   "source": [
    "# ？\n",
    "\n",
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card:\"))\n",
    "count = 0\n",
    "\n",
    "if hide_card <100 and hide_card >0:\n",
    "    while True:\n",
    "        if count <5:\n",
    "            if guess_digit <100 and guess_digit >0:\n",
    "                guess_digit = int(input(\"guess_digit:\"))\n",
    "                if guess_digit == hide_card:\n",
    "                    print(\"yes, you got it.\")\n",
    "                    break\n",
    "                elif guess_digit > hide_card:\n",
    "                    print(\"think smaller...\")\n",
    "                else:\n",
    "                    print(\"think bigger...\")\n",
    "                count +=1\n",
    "            else:\n",
    "                print(\"请输入数字在0-99之间。不要耍赖！\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜这么多次都不对,你个笨蛋!\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "            \n",
    "else:\n",
    "    print(\"请输入数字在0-99之间。不要耍赖！\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
